package com.bops.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;



public class CheckUpload {
	public static Integer imageUpload(MultipartFile file,String imgDisk){
		    int len =0;
			try {
			FileOutputStream fos = new FileOutputStream(imgDisk+file.getOriginalFilename());
			InputStream fis = file.getInputStream();
			byte[] buffer = new byte[1024];
			while((len = fis.read(buffer)) >0)
			{
				fos.write(buffer,0,len);
			}
			} catch (Exception e) {
				e.printStackTrace();
			}
		return len;
		
	}
	
	public static String checkImg(MultipartFile file,String imgDisk){
		 String msg ="success";
		 if (file == null || file.getSize() == 0) {
			  msg="上传的图片是空!";
			  return msg;
	        }
	        if (file.getSize() > 500 * 1024) {
	        	 msg="上传图片不能大于500k!";
	        	 return msg;
	        }
	        File pic = new File(imgDisk);
	        if(pic != null){
				for (int i = 0; i < pic.list().length; i++) {
					if(pic.list()[i].equals(file.getOriginalFilename())){
						 msg="已存在相同的图片名!";
						return msg;
					}
				}
	        }
		return msg;
	}
}
